package stz.flash.main.strobe.model;

import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.CodeModuleManager;
import stz.flash.main.FlashKeyEvent;
import stz.flash.main.ScreenHandle;
import util.DjDoneListener;


public class VRController{
	
	private static final int DELAY_VR_FOREGROUND_CHECK = 80;
	private static final int DELAY_AFTER_VR_FOREGROUND = 550;//VR이 안정적으로 구동될수 있도록 시간을 줌

	private static final int REPEAT_NUM_POST_ESC_KEY = 5;
	private static final int DELAY_AFTER_POST_ESC_KEY = 100;
	
	private static final int REPEAT_NUM_IS_READY_VR = 15;
	private static final int DELAY_IS_READY_VR = 100;

	private static Object lock = new Object();
	private static boolean workingThread;
	
	public static final ApplicationDescriptor VRDescr = 
			CodeModuleManager.getApplicationDescriptors(
					CodeModuleManager .getModuleHandle("net_rim_bb_videorecorder"))[0];
	private static final ApplicationManager appManager = 
			ApplicationManager.getApplicationManager();
	
	
	public static void runVideoRecorder(final DjDoneListener doneListener) {
	
		new Thread() {
			public void run() {
				try {
					if (readyVideoRecoder() == false) { 
						int id = ApplicationManager.getApplicationManager().
								runApplication(VRDescr, true);
						waitVRForeground(id);
					}

					if (doneListener != null)
						doneListener.done();
					

				} catch (Exception e) {
				}
			}
			
		}.start();
	}
	private static boolean readyVideoRecoder() throws InterruptedException  {
		return isReadyVR() && isVRForeground();
	}
	private static void waitVRForeground(int processId) throws InterruptedException {

		for (int i = 0; i < 30; i++){
			
			Thread.sleep(DELAY_VR_FOREGROUND_CHECK);

			if (isVRBackground()) {
				ApplicationManager.getApplicationManager().requestForeground(processId);
			}
			
			if (isVRForeground()) {
				Thread.sleep(DELAY_AFTER_VR_FOREGROUND);
				VRFlashController.popScreen();
				InspectVRFlash.waitVisibleVR();
				return;
			}
		}
	}
	
	public static void closeVR(final DjDoneListener doneListener) {
		new Thread() {
			public void run() {
				try {

					synchronized (lock) {
						if (workingThread == false) 
							workingThread = true;
						else
							return;
					}

					for (int i = 0; i < REPEAT_NUM_POST_ESC_KEY; i++) {

						ScreenHandle.getIns().popScreen();
						FlashKeyEvent.postEscKey();
						Thread.sleep(DELAY_AFTER_POST_ESC_KEY);
						ScreenHandle.getIns().pushScreen();

						for (int j = 0; j < REPEAT_NUM_IS_READY_VR; j++) {

							try {
								Thread.sleep(DELAY_IS_READY_VR);
							} catch (InterruptedException e) {
							}
							if (isReadyVR()) 
								continue;
						}

						if (doneListener != null) {
							doneListener.done();
						}

						break;
					}
					workingThread = false;
				} catch (InterruptedException e) {
				}
			}
		}.start();
	}
	
	private static boolean isReadyVR() {
		return ApplicationManager.getApplicationManager().getProcessId(VRDescr) != -1;
	}
	
	private static boolean isVRBackground() {
		return isReadyVR() && isVRForeground() == false;
	}
	
	public static boolean isVRForeground(){
		return appManager.getForegroundProcessId() ==
				appManager.getProcessId(VRDescr);
	}
}
